#include "LED.h"

void led_on(LED_TYPE type)
{
	if (type == RED_LED) {
			HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8,GPIO_PIN_RESET);
}
	else if (type == GREEN_LED) {
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}
	}
void led_off(LED_TYPE type)
{
	if (type == RED_LED) {
			HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8,GPIO_PIN_SET);
}
	else if (type == GREEN_LED) {
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
}
	}
void led_blink(LED_TYPE type,uint32_t millisecond,uint32_t times)
{
	int i;
	for(i=0;i<times;i++)
	{
		if (type==RED_LED)
		{
			HAL_GPIO_TogglePin (GPIOA,GPIO_PIN_8);
		}
		else if (type==GREEN_LED){
			HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_2);
		}
		HAL_Delay(millisecond);
	}
}

void led_breathing(LED_TYPE type,uint32_t millisecond,uint32_t times)
{
	int vector =1;
	int ccr = 0;
	int period = 1000;
	int delay = (int)(((float)period /(millisecond/2)) * 20);
	
	while(times)
	{
		if(vector)ccr +=delay;
		else ccr-=delay; 
		if(ccr>1000) vector=0;
		if(ccr<=0) {
			vector=1;
		TIM1->CCR1 = ccr;
		HAL_Delay(20);
	
	}

}
}
LED led ={
	.on =led_on,
	.off=led_off,
	.blink=led_blink,
	.breathing=led_breathing,
};  